package com.cyt.web.controller.work.antiepidemic.phone;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.cyt.base.enums.BusinessType;
import com.cyt.base.enums.OperatorType;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.DictCode;
import com.cyt.base.util.StringUtils;
import com.cyt.sys.aspect.BaseLog;
import com.cyt.sys.model.po.SysDictContent;
import com.cyt.sys.util.cache.DictUtils;
import com.cyt.sys.util.cache.ParamConfigUtils;
import com.cyt.web.controller.base.PhoneController;
import com.cyt.work.antiepidemic.constant.AntiepidemicConstant;
import com.cyt.work.antiepidemic.dao.AntiepidemicRecordDao;
import com.cyt.work.antiepidemic.model.po.AntiepidemicBuilding;
import com.cyt.work.antiepidemic.model.po.AntiepidemicCommunity;
import com.cyt.work.antiepidemic.model.po.AntiepidemicPerson;
import com.cyt.work.antiepidemic.model.po.AntiepidemicRecord;
import com.cyt.work.antiepidemic.model.vo.AntiepidemicCommunityVo;
import com.cyt.work.antiepidemic.model.vo.AntiepidemicPersonVo;
import com.cyt.work.antiepidemic.model.vo.AntiepidemicRecordVo;
import com.cyt.work.antiepidemic.model.vo.AntiepidemicTemperatureVo;
import com.cyt.work.antiepidemic.service.*;
import com.cyt.wx.model.po.WxMember;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author rong yuan
 * @date 2020/2/12 2:45
 */
@RestController
public class AntiepidemicController extends PhoneController {

    private static final long MAX_SHOW_TIME = 30 * 60;

    @Resource
    private AntiepidemicPersonService antiepidemicPersonService;
    @Resource
    private AntiepidemicRecordService antiepidemicRecordService;
    @Resource
    private AntiepidemicBuildingService antiepidemicBuildingService;
    @Resource
    private AntiepidemicCommunityService antiepidemicCommunityService;
    @Resource
    private AntiepidemicRecordDao antiepidemicRecordDao;
    @Resource
    private AntiepidemicTemperatureService antiepidemicTemperatureService;

    /**
     * 跳转小区列表
     *
     * @param token   用户信息
     * @param applyId 应用ID
     * @return ModelAndView
     * @author rong yuan
     * @date 2020/2/12 2:45
     */
    @RequestMapping(value = "/phone/antiepidemic/list", method = RequestMethod.GET)
    public ModelAndView antiepidemicList(String token, String applyId) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("token", token);
        modelAndView.addObject("applyId", applyId);
        List<AntiepidemicCommunity> list = antiepidemicCommunityService.findList(new AntiepidemicCommunityVo());
        modelAndView.addObject("list", list);
        modelAndView.setViewName("/mobile/antiepidemic/index");
        return modelAndView;
    }

    /**
     * 跳转首页或注册界面
     *
     * @param token       用户信息
     * @param applyId     应用ID
     * @param communityId 社区ID
     * @return ModelAndView
     * @author rong yuan
     * @date 2020/2/12 2:45
     */
    @RequestMapping(value = "/phone/antiepidemic/index", method = RequestMethod.GET)
    public ModelAndView antiepidemicIndex(String token, String applyId, String communityId) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("token", token);
        modelAndView.addObject("applyId", applyId);
        modelAndView.addObject("communityId", communityId);

        AntiepidemicCommunity antiepidemicCommunity = antiepidemicCommunityService.selectByPrimaryKey(communityId);
        modelAndView.addObject("communityType", antiepidemicCommunity.getCommunityType());
        modelAndView.addObject("checkType", antiepidemicCommunity.getCheckType());

        WxMember wxMember = acceptToken(token);
        AntiepidemicPerson antiepidemicPerson = new AntiepidemicPerson();
        antiepidemicPerson.setApplyId(applyId);
        antiepidemicPerson.setMemberId(wxMember.getId());
        antiepidemicPerson.setRemoveTag(DictCode.BASE_REMOVE_TAG_NORMAL);
        antiepidemicPerson = antiepidemicPersonService.selectOne(antiepidemicPerson);
        if (antiepidemicPerson != null) {
            if (AntiepidemicConstant.VERIFY_STATUS_DOING.equals(antiepidemicPerson.getVerifyStatus())) {
                modelAndView.setViewName("/mobile/antiepidemic/checking");
            } else if (AntiepidemicConstant.VERIFY_STATUS_SUCCESS.equals(antiepidemicPerson.getVerifyStatus())) {
                modelAndView.addObject("personId", antiepidemicPerson.getId());
                modelAndView.setViewName("/mobile/antiepidemic/antiepidemicMenu");
            } else if (AntiepidemicConstant.VERIFY_STATUS_FAILURE.equals(antiepidemicPerson.getVerifyStatus())) {
                modelAndView.setViewName("/mobile/antiepidemic/checkFailure");
            }
        } else {
            modelAndView.addObject("memberId", wxMember.getId());
            modelAndView.setViewName("/mobile/antiepidemic/register");
        }
        return modelAndView;
    }

    /**
     * 跳转审核界面
     *
     * @return ModelAndView
     * @author rong yuan
     * @date 2020/2/12 2:45
     */
    @RequestMapping(value = "/phone/antiepidemic/checking", method = RequestMethod.GET)
    public ModelAndView antiepidemicChecking() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/mobile/antiepidemic/checking");
        return modelAndView;
    }

    /**
     * 注册登记
     *
     * @param antiepidemicPersonVo 接受参数对象
     * @return ReturnResult
     * @author rong yuan
     * @date 2020-2-12 2:07:59
     */
    @BaseLog(module = "e门通", classz = "社区住户信息", methods = "注册登记", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/phone/antiepidemic/person/addOrUpdate", method = RequestMethod.POST)
    public ReturnResult addOrUpdate(AntiepidemicPersonVo antiepidemicPersonVo) {
        antiepidemicPersonVo.setCertificateStatus(AntiepidemicConstant.CERTIFICATE_STATUS_NO);
        return antiepidemicPersonService.addOrUpdate(antiepidemicPersonVo, null);
    }

    /**
     * 跳转外出登记
     *
     * @param token    用户信息
     * @param applyId  应用ID
     * @param personId 住户ID
     * @return ReturnResult
     * @author rong yuan
     * @date 2020-2-12 2:24:31
     */
    @BaseLog(module = "e门通", classz = "社区出入信息", methods = "跳转外出登记", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/phone/antiepidemic/record/out", method = RequestMethod.GET)
    public ModelAndView recordOut(String token, String applyId, String personId) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("token", token);
        modelAndView.addObject("applyId", applyId);
        modelAndView.addObject("personId", personId);

        //校验是否隔离
        AntiepidemicPerson antiepidemicPerson = antiepidemicPersonService.selectByPrimaryKey(personId);
        if (AntiepidemicConstant.RETURN_STATUS_YES.equals(antiepidemicPerson.getReturnStatus())) {
            long actualHour = DateUtil.between(antiepidemicPerson.getReturnTime(), new Date(), DateUnit.HOUR);
            long divideHour = Long.parseLong(ParamConfigUtils.getParamConfig("divideDay").getInfo()) * 24;
            if (actualHour < divideHour) {
                modelAndView.addObject("day", (divideHour - actualHour) / 24);
                modelAndView.addObject("hour", (divideHour - actualHour) % 24);
                modelAndView.setViewName("/mobile/antiepidemic/antiepidemicOutLimit");
                return modelAndView;
            }
        }

        //校验是否已外出
        AntiepidemicRecord antiepidemicRecord = new AntiepidemicRecord();
        antiepidemicRecord.setApplyId(applyId);
        antiepidemicRecord.setPersonId(personId);
        antiepidemicRecord.setAccessStatus(AntiepidemicConstant.ACCESS_STATUS_NO);
        antiepidemicRecord.setRemoveTag(DictCode.BASE_REMOVE_TAG_NORMAL);
        antiepidemicRecord = antiepidemicRecordService.selectOne(antiepidemicRecord);
        if (antiepidemicRecord != null) {
            modelAndView.setViewName("/mobile/antiepidemic/antiepidemicOuted");
            return modelAndView;
        }

        AntiepidemicBuilding antiepidemicBuilding = antiepidemicBuildingService.selectByPrimaryKey(antiepidemicPerson.getBuildingId());
        AntiepidemicCommunity antiepidemicCommunity = antiepidemicCommunityService.selectByPrimaryKey(antiepidemicBuilding.getCommunityId());
        //校验该户外出次数
        if (checkMaxTripTimes(applyId, personId, antiepidemicPerson.getBuildingId(), antiepidemicPerson.getBuildingRoom(), antiepidemicCommunity.getMaxTripTimes())) {
            modelAndView.addObject("day", antiepidemicCommunity.getMaxTripTimes().split("/")[0]);
            modelAndView.addObject("times", antiepidemicCommunity.getMaxTripTimes().split("/")[1]);
            modelAndView.setViewName("/mobile/antiepidemic/antiepidemicOutMax");
            return modelAndView;
        }

        //正常外出登记
        AntiepidemicRecordVo antiepidemicRecordVo = new AntiepidemicRecordVo();
        antiepidemicRecordVo.setApplyId(applyId);
        antiepidemicRecordVo.setPersonId(personId);
        antiepidemicRecordVo.setAccessType(antiepidemicPerson.getCertificateStatus());
        antiepidemicRecordVo.setBeginTime(DateUtil.formatDateTime(new Date()));
        antiepidemicRecordVo.setAccessStatus(AntiepidemicConstant.ACCESS_STATUS_NO);
        antiepidemicRecordService.addOrUpdate(antiepidemicRecordVo, null);

        modelAndView.addObject("buildingInfo", antiepidemicCommunity.getBuildingName() + antiepidemicBuilding.getBuildingInfo() + antiepidemicPerson.getBuildingRoom());
        String beginTime = antiepidemicRecordVo.getBeginTime();
        modelAndView.addObject("date", beginTime.substring(0, 4) + "年" + beginTime.substring(5, 7) + "月" + beginTime.substring(8, 10) + "日");
        modelAndView.addObject("time", beginTime.substring(11, 16));
        if (AntiepidemicConstant.CERTIFICATE_STATUS_NO.equals(antiepidemicPerson.getCertificateStatus())) {
            modelAndView.setViewName("/mobile/antiepidemic/antiepidemicOutOk");
        } else if (AntiepidemicConstant.CERTIFICATE_STATUS_YES.equals(antiepidemicPerson.getCertificateStatus())) {
            if (StringUtils.isNotBlank(antiepidemicPerson.getCertificateTime())) {
                modelAndView.addObject("certificateTime", antiepidemicPerson.getCertificateTime().substring(0, 5));
                modelAndView.setViewName("/mobile/antiepidemic/antiepidemicOutOkFg");
            } else {
                modelAndView.setViewName("/mobile/antiepidemic/antiepidemicOutOk");
            }
        }
        return modelAndView;
    }

    private boolean checkMaxTripTimes(String applyId, String personId, String buildingId, String buildingRoom, String maxTripTimes) {
        AntiepidemicPersonVo antiepidemicPersonVo = new AntiepidemicPersonVo();
        antiepidemicPersonVo.setApplyId(applyId);
        antiepidemicPersonVo.setBuildingId(buildingId);
        antiepidemicPersonVo.setBuildingRoom(buildingRoom);
        List<AntiepidemicPerson> antiepidemicPersonList = antiepidemicPersonService.listExample(antiepidemicPersonVo);

        List<String> personIdList = new ArrayList<>();
        for (AntiepidemicPerson person : antiepidemicPersonList) {
            if (!personIdList.contains(person.getId())) {
                personIdList.add(person.getId());
            }
        }

        AntiepidemicRecordVo antiepidemicRecordVo = new AntiepidemicRecordVo();
        antiepidemicRecordVo.setApplyId(applyId);
        antiepidemicRecordVo.setPersonId(String.join(",", personIdList));
        antiepidemicRecordVo.setAccessType(AntiepidemicConstant.CERTIFICATE_STATUS_NO);
        int day = Integer.parseInt(maxTripTimes.split("/")[0]);
        int times = Integer.parseInt(maxTripTimes.split("/")[1]);
        antiepidemicRecordVo.setStartTime(DateUtil.formatDate(DateUtil.beginOfDay(DateUtil.offsetDay(new Date(), 1 - day))));
        antiepidemicRecordVo.setEndTime(DateUtil.formatDate(DateUtil.endOfDay(new Date())));
        ReturnResult returnResult = antiepidemicRecordService.page(antiepidemicRecordVo, 1, 1);
        if (returnResult.getCount() >= times) {
            return true;
        }
        antiepidemicRecordVo = new AntiepidemicRecordVo();
        antiepidemicRecordVo.setApplyId(applyId);
        antiepidemicRecordVo.setPersonId(personId);
        antiepidemicRecordVo.setAccessType(AntiepidemicConstant.CERTIFICATE_STATUS_YES);
        antiepidemicRecordVo.setStartTime(DateUtil.formatDate(DateUtil.beginOfDay(new Date())));
        antiepidemicRecordVo.setEndTime(DateUtil.formatDate(DateUtil.endOfDay(new Date())));
        returnResult = antiepidemicRecordService.page(antiepidemicRecordVo, 1, 1);
        return returnResult.getCount() != 0;
    }

    /**
     * 跳转进入登记
     *
     * @param token    用户信息
     * @param applyId  应用ID
     * @param personId 住户ID
     * @return ReturnResult
     * @author rong yuan
     * @date 2020-2-12 2:24:31
     */
    @BaseLog(module = "e门通", classz = "社区出入信息", methods = "跳转进入登记", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/phone/antiepidemic/record/back", method = RequestMethod.GET)
    public ModelAndView recordBack(String token, String applyId, String personId) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("token", token);
        modelAndView.addObject("applyId", applyId);
        modelAndView.addObject("personId", personId);

        //校验是否已进入
        AntiepidemicRecord antiepidemicRecord = new AntiepidemicRecord();
        antiepidemicRecord.setApplyId(applyId);
        antiepidemicRecord.setPersonId(personId);
        antiepidemicRecord.setRemoveTag(DictCode.BASE_REMOVE_TAG_NORMAL);
        List<AntiepidemicRecord> antiepidemicRecordList = antiepidemicRecordService.select(antiepidemicRecord);
        if (CollectionUtils.isEmpty(antiepidemicRecordList)) {
            modelAndView.setViewName("/mobile/antiepidemic/antiepidemicBacked");
            return modelAndView;
        }
        antiepidemicRecordList.sort((o1, o2) -> {
            if (o1.getBeginTime().getTime() > o2.getBeginTime().getTime()) {
                return -1;
            } else if (o1.getBeginTime().getTime() < o2.getBeginTime().getTime()) {
                return 1;
            }
            return 0;
        });

        antiepidemicRecord = antiepidemicRecordList.get(0);
        if (antiepidemicRecord.getEndTime() == null) {
            antiepidemicRecord.setEndTime(new Date());
        }

        AntiepidemicPerson antiepidemicPerson = antiepidemicPersonService.selectByPrimaryKey(personId);
        //正常进入登记
        if (AntiepidemicConstant.CERTIFICATE_STATUS_NO.equals(antiepidemicRecord.getAccessType())) {
            long actualMinute = DateUtil.between(antiepidemicRecord.getBeginTime(), antiepidemicRecord.getEndTime(), DateUnit.MINUTE);
            long divideMinute = Long.parseLong(ParamConfigUtils.getParamConfig("divideHour").getInfo()) * 60;
            modelAndView.addObject("divideHour", divideMinute / 60);
            if (actualMinute > divideMinute) {
                modelAndView.setViewName("/mobile/antiepidemic/antiepidemicBackOver");
                antiepidemicRecord.setAccessStatus(AntiepidemicConstant.ACCESS_STATUS_OVERTIME);
            } else {
                modelAndView.setViewName("/mobile/antiepidemic/antiepidemicBackOk");
                antiepidemicRecord.setAccessStatus(AntiepidemicConstant.ACCESS_STATUS_YES);
            }
        } else if (AntiepidemicConstant.CERTIFICATE_STATUS_YES.equals(antiepidemicRecord.getAccessType())) {
            if (StringUtils.isBlank(antiepidemicPerson.getCertificateTime())) {
                modelAndView.setViewName("/mobile/antiepidemic/antiepidemicBackOkFg");
                antiepidemicRecord.setAccessStatus(AntiepidemicConstant.ACCESS_STATUS_YES);
            } else {
                Date date = DateUtil.parseDateTime(DateUtil.formatDate(antiepidemicRecord.getBeginTime()) + " " + antiepidemicPerson.getCertificateTime().substring(0, 5) + ":00");
                long actualMinute = DateUtil.between(date, antiepidemicRecord.getEndTime(), DateUnit.SECOND, false);
                modelAndView.addObject("certificateTime", antiepidemicPerson.getCertificateTime().substring(0, 5));
                if (actualMinute < 0) {
                    antiepidemicRecord.setEndTime(new Date());
                    if (date.getTime() > System.currentTimeMillis()) {
                        modelAndView.setViewName("/mobile/antiepidemic/antiepidemicBackOverFg");
                        antiepidemicRecord.setAccessStatus(AntiepidemicConstant.ACCESS_STATUS_ADVANCE);
                    } else {
                        modelAndView.setViewName("/mobile/antiepidemic/antiepidemicBackOkFg");
                        antiepidemicRecord.setAccessStatus(AntiepidemicConstant.ACCESS_STATUS_YES);
                    }
                } else {
                    modelAndView.setViewName("/mobile/antiepidemic/antiepidemicBackOkFg");
                    antiepidemicRecord.setAccessStatus(AntiepidemicConstant.ACCESS_STATUS_YES);
                }
            }
        }
        AntiepidemicBuilding antiepidemicBuilding = antiepidemicBuildingService.selectByPrimaryKey(antiepidemicPerson.getBuildingId());
        AntiepidemicCommunity antiepidemicCommunity = antiepidemicCommunityService.selectByPrimaryKey(antiepidemicBuilding.getCommunityId());
        modelAndView.addObject("buildingInfo", antiepidemicCommunity.getBuildingName() + antiepidemicBuilding.getBuildingInfo() + antiepidemicPerson.getBuildingRoom());
        String beginTime = DateUtil.formatDateTime(antiepidemicRecord.getBeginTime());
        modelAndView.addObject("beginTime", beginTime.substring(0, 4) + "年" + beginTime.substring(5, 7) + "月" + beginTime.substring(8, 10) + "日" + beginTime.substring(11, 16));
        String endTime = DateUtil.formatDateTime(antiepidemicRecord.getEndTime());
        modelAndView.addObject("endTime", endTime.substring(0, 4) + "年" + endTime.substring(5, 7) + "月" + endTime.substring(8, 10) + "日" + endTime.substring(11, 16));
        long minute = DateUtil.between(DateUtil.parseDateTime(DateUtil.formatDateTime(antiepidemicRecord.getBeginTime()).substring(0, 16) + ":00"), DateUtil.parseDateTime(DateUtil.formatDateTime(antiepidemicRecord.getEndTime()).substring(0, 16) + ":00"), DateUnit.MINUTE);
        modelAndView.addObject("hour", minute / 60);
        modelAndView.addObject("minute", minute % 60);
        antiepidemicRecordDao.updateByPrimaryKey(antiepidemicRecord);
        return modelAndView;
    }

    /**
     * 跳转体温上报
     *
     * @param token       用户信息
     * @param applyId     应用ID
     * @param communityId 社区ID
     * @param personId    住户ID
     * @return ReturnResult
     * @author rong yuan
     * @date 2020-2-12 2:24:31
     */
    @BaseLog(module = "e门通", classz = "社区出入信息", methods = "跳转体温上报", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/phone/antiepidemic/temperature", method = RequestMethod.GET)
    public ModelAndView temperature(String token, String applyId, String communityId, String personId) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("token", token);
        modelAndView.addObject("applyId", applyId);
        modelAndView.addObject("communityId", communityId);
        modelAndView.addObject("personId", personId);

        AntiepidemicPerson antiepidemicPerson = antiepidemicPersonService.selectByPrimaryKey(personId);
        AntiepidemicBuilding antiepidemicBuilding = antiepidemicBuildingService.selectByPrimaryKey(antiepidemicPerson.getBuildingId());
        AntiepidemicCommunity antiepidemicCommunity = antiepidemicCommunityService.selectByPrimaryKey(antiepidemicBuilding.getCommunityId());

        List<SysDictContent> sysDictContentList;

        modelAndView.addObject("name", antiepidemicPerson.getName());
        sysDictContentList = DictUtils.getBaseDictViewVo("sexType").getSysDictContentList();
        if (CollectionUtils.isNotEmpty(sysDictContentList)) {
            modelAndView.addObject("sexType", "");
            for (SysDictContent sysDictContent : sysDictContentList) {
                if (sysDictContent.getValue().equals(antiepidemicPerson.getSexType())) {
                    modelAndView.addObject("sexType", sysDictContent.getName());
                    break;
                }
            }
        }
        String stringBuilder = antiepidemicCommunity.getProvinceCode() + antiepidemicCommunity.getCityCode() +
                antiepidemicCommunity.getRegionCode() + antiepidemicCommunity.getBuildingName() +
                antiepidemicBuilding.getBuildingInfo() + antiepidemicPerson.getBuildingRoom();
        modelAndView.addObject("buildingId", stringBuilder);
        modelAndView.setViewName("/mobile/antiepidemic/temperature");
        return modelAndView;
    }

    /**
     * 健康上传
     *
     * @param antiepidemicTemperatureVo 接受参数对象
     * @return ReturnResult
     * @author rong yuan
     * @date 2020-2-12 2:07:59
     */
    @BaseLog(module = "e门通", classz = "健康上传", methods = "健康上传", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/phone/antiepidemic/temperature/addOrUpdate", method = RequestMethod.POST)
    public ReturnResult addOrUpdate(AntiepidemicTemperatureVo antiepidemicTemperatureVo) {
        antiepidemicTemperatureVo.setRecordTime(DateUtil.formatDateTime(new Date()));
        return antiepidemicTemperatureService.addOrUpdate(antiepidemicTemperatureVo, null);
    }

    /**
     * 跳转注册登记
     *
     * @param token    用户信息
     * @param applyId  应用ID
     * @param personId 住户ID
     * @return ReturnResult
     * @author rong yuan
     * @date 2020-2-12 2:24:31
     */
    @BaseLog(module = "e门通", classz = "社区住户信息", methods = "跳转注册登记", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/phone/antiepidemic/registerDetail", method = RequestMethod.GET)
    public ModelAndView registerDetail(String token, String applyId, String personId) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("token", token);
        modelAndView.addObject("applyId", applyId);
        modelAndView.addObject("personId", personId);

        AntiepidemicPerson antiepidemicPerson = antiepidemicPersonService.selectByPrimaryKey(personId);
        AntiepidemicBuilding antiepidemicBuilding = antiepidemicBuildingService.selectByPrimaryKey(antiepidemicPerson.getBuildingId());
        AntiepidemicCommunity antiepidemicCommunity = antiepidemicCommunityService.selectByPrimaryKey(antiepidemicBuilding.getCommunityId());

        List<SysDictContent> sysDictContentList;

        modelAndView.addObject("name", antiepidemicPerson.getName());
        sysDictContentList = DictUtils.getBaseDictViewVo("sexType").getSysDictContentList();
        if (CollectionUtils.isNotEmpty(sysDictContentList)) {
            modelAndView.addObject("sexType", "");
            for (SysDictContent sysDictContent : sysDictContentList) {
                if (sysDictContent.getValue().equals(antiepidemicPerson.getSexType())) {
                    modelAndView.addObject("sexType", sysDictContent.getName());
                    break;
                }
            }
        }
        modelAndView.addObject("mobile", antiepidemicPerson.getMobile());
        modelAndView.addObject("idCard", antiepidemicPerson.getIdCard());
        modelAndView.addObject("carNumber", antiepidemicPerson.getCarNumber());
        String stringBuilder = antiepidemicCommunity.getProvinceCode() + antiepidemicCommunity.getCityCode() + antiepidemicCommunity.getRegionCode() +
                antiepidemicCommunity.getBuildingName() + antiepidemicBuilding.getBuildingInfo();
        modelAndView.addObject("buildingId", stringBuilder);
        modelAndView.addObject("buildingRoom", antiepidemicPerson.getBuildingRoom());
        sysDictContentList = DictUtils.getBaseDictViewVo("returnStatus").getSysDictContentList();
        if (CollectionUtils.isNotEmpty(sysDictContentList)) {
            modelAndView.addObject("returnStatus", "");
            for (SysDictContent sysDictContent : sysDictContentList) {
                if (sysDictContent.getValue().equals(antiepidemicPerson.getReturnStatus())) {
                    modelAndView.addObject("returnStatus", sysDictContent.getName());
                    break;
                }
            }
        }
        modelAndView.addObject("returnTime", DateUtil.formatDate(antiepidemicPerson.getReturnTime()));
        modelAndView.addObject("returnArea", antiepidemicPerson.getReturnArea());
        sysDictContentList = DictUtils.getBaseDictViewVo("epidemicArea").getSysDictContentList();
        if (CollectionUtils.isNotEmpty(sysDictContentList)) {
            modelAndView.addObject("epidemicArea", "");
            for (SysDictContent sysDictContent : sysDictContentList) {
                if (sysDictContent.getValue().equals(antiepidemicPerson.getEpidemicArea())) {
                    modelAndView.addObject("epidemicArea", sysDictContent.getName());
                    break;
                }
            }
        }
        modelAndView.setViewName("/mobile/antiepidemic/registerDetail");
        return modelAndView;
    }

    /**
     * 小区地址选择器
     *
     * @param applyId     应用ID
     * @param communityId 社区ID
     * @return ReturnResult
     * @author rong yuan
     * @date 2020-2-12 2:07:59
     */
    @BaseLog(module = "e门通", classz = "小区地址选择器", methods = "小区地址选择器", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/phone/antiepidemic/building/list", method = RequestMethod.POST)
    public ReturnResult buildingList(String applyId, String communityId) {
        AntiepidemicBuilding antiepidemicBuilding = new AntiepidemicBuilding();
        antiepidemicBuilding.setApplyId(applyId);
        antiepidemicBuilding.setCommunityId(communityId);
        antiepidemicBuilding.setRemoveTag(DictCode.BASE_REMOVE_TAG_NORMAL);
        List<AntiepidemicBuilding> antiepidemicBuildingList = antiepidemicBuildingService.select(antiepidemicBuilding);
        antiepidemicBuildingList.sort((o1, o2) -> {
            if (o1.getOrderNum() > o2.getOrderNum()) {
                return 1;
            } else if (o1.getOrderNum() < o2.getOrderNum()) {
                return -1;
            }
            return 0;
        });
        List<Map<String, String>> mapList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(antiepidemicBuildingList)) {
            for (AntiepidemicBuilding building : antiepidemicBuildingList) {
                Map<String, String> map = new HashMap<>();
                map.put("value", building.getId());
                map.put("title", building.getBuildingInfo());
                mapList.add(map);
            }
        }
        return new ReturnResult(mapList);
    }

    /**
     * 跳转进入登记（写字楼）
     *
     * @param token    用户信息
     * @param applyId  应用ID
     * @param personId 住户ID
     * @return ReturnResult
     * @author rong yuan
     * @date 2020-2-12 2:24:31
     */
    @BaseLog(module = "e门通", classz = "社区出入信息", methods = "跳转进入登记（写字楼）", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/phone/antiepidemic/record/work/back", method = RequestMethod.GET)
    public ModelAndView recordWorkBack(String token, String applyId, String personId) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("token", token);
        modelAndView.addObject("applyId", applyId);
        modelAndView.addObject("personId", personId);

        //楼栋信息
        Date currentDate = new Date();
        AntiepidemicPerson antiepidemicPerson = antiepidemicPersonService.selectByPrimaryKey(personId);
        AntiepidemicBuilding antiepidemicBuilding = antiepidemicBuildingService.selectByPrimaryKey(antiepidemicPerson.getBuildingId());
        AntiepidemicCommunity antiepidemicCommunity = antiepidemicCommunityService.selectByPrimaryKey(antiepidemicBuilding.getCommunityId());
        modelAndView.addObject("buildingInfo", antiepidemicCommunity.getBuildingName() + antiepidemicBuilding.getBuildingInfo() + antiepidemicPerson.getBuildingRoom());
        //校验是否已经进入
        AntiepidemicRecord antiepidemicRecord = new AntiepidemicRecord();
        antiepidemicRecord.setApplyId(applyId);
        antiepidemicRecord.setPersonId(personId);
        antiepidemicRecord.setRemoveTag(DictCode.BASE_REMOVE_TAG_NORMAL);
        List<AntiepidemicRecord> antiepidemicRecordList = antiepidemicRecordService.select(antiepidemicRecord);
        if (CollectionUtils.isNotEmpty(antiepidemicRecordList)) {
            antiepidemicRecordList.sort((o1, o2) -> {
                if (o1.getBeginTime().getTime() > o2.getBeginTime().getTime()) {
                    return -1;
                } else if (o1.getBeginTime().getTime() < o2.getBeginTime().getTime()) {
                    return 1;
                }
                return 0;
            });
            antiepidemicRecord = antiepidemicRecordList.get(0);
            if (DateUtil.formatDate(new Date()).equals(DateUtil.formatDate(antiepidemicRecord.getBeginTime()))) {
                if (DateUtil.between(antiepidemicRecord.getBeginTime(), currentDate, DateUnit.SECOND, false) > MAX_SHOW_TIME) {
                    modelAndView.setViewName("/mobile/antiepidemic/work/antiepidemicBacked");
                    return modelAndView;
                } else {
                    String beginTime = DateUtil.formatDateTime(antiepidemicRecord.getBeginTime());
                    modelAndView.addObject("date", beginTime.substring(0, 4) + "年" + beginTime.substring(5, 7) + "月" + beginTime.substring(8, 10) + "日");
                    modelAndView.addObject("time", beginTime.substring(11, 16));
                    modelAndView.addObject("offStartWork", antiepidemicCommunity.getOffStartWork().substring(0, 5));
                    modelAndView.addObject("offEndWork", antiepidemicCommunity.getOffEndWork().substring(0, 5));
                    modelAndView.setViewName("/mobile/antiepidemic/work/antiepidemicBackOk");
                    return modelAndView;
                }
            } else {
                if (AntiepidemicConstant.ACCESS_STATUS_NO.equals(antiepidemicRecord.getAccessType())) {
                    modelAndView.setViewName("/mobile/antiepidemic/work/antiepidemicBacked");
                    return modelAndView;
                }
            }
        }
        //检验进入时间是否符合要求
        String beginTime = DateUtil.formatDateTime(currentDate);
        modelAndView.addObject("date", beginTime.substring(0, 4) + "年" + beginTime.substring(5, 7) + "月" + beginTime.substring(8, 10) + "日");
        modelAndView.addObject("time", beginTime.substring(11, 16));
        Date nowDate = DateUtil.parseDateTime(beginTime + ":00");
        Date goStartWork = DateUtil.parseDateTime(DateUtil.formatDate(nowDate) + " " + antiepidemicCommunity.getGoStartWork().substring(0, 5) + ":00");
        Date goEndWork = DateUtil.parseDateTime(DateUtil.formatDate(nowDate) + " " + antiepidemicCommunity.getGoEndWork().substring(0, 5) + ":00");
        if (DateUtil.between(goStartWork, nowDate, DateUnit.MINUTE, false) < 0) {
            modelAndView.addObject("goStartWork", antiepidemicCommunity.getGoStartWork().substring(0, 5));
            modelAndView.addObject("goEndWork", antiepidemicCommunity.getGoEndWork().substring(0, 5));
            modelAndView.setViewName("/mobile/antiepidemic/work/antiepidemicBackAdvance");
        } else if (DateUtil.between(goEndWork, nowDate, DateUnit.MINUTE, false) > 0) {
            modelAndView.addObject("goStartWork", antiepidemicCommunity.getGoStartWork().substring(0, 5));
            modelAndView.addObject("goEndWork", antiepidemicCommunity.getGoEndWork().substring(0, 5));
            modelAndView.setViewName("/mobile/antiepidemic/work/antiepidemicBackOver");
        } else {
            modelAndView.addObject("offStartWork", antiepidemicCommunity.getOffStartWork().substring(0, 5));
            modelAndView.addObject("offEndWork", antiepidemicCommunity.getOffEndWork().substring(0, 5));
            modelAndView.setViewName("/mobile/antiepidemic/work/antiepidemicBackOk");
            AntiepidemicRecordVo antiepidemicRecordVo = new AntiepidemicRecordVo();
            antiepidemicRecordVo.setApplyId(applyId);
            antiepidemicRecordVo.setPersonId(personId);
            antiepidemicRecordVo.setAccessType(AntiepidemicConstant.CERTIFICATE_STATUS_YES);
            antiepidemicRecordVo.setBeginTime(beginTime);
            antiepidemicRecordVo.setAccessStatus(AntiepidemicConstant.ACCESS_STATUS_NO);
            antiepidemicRecordService.addOrUpdate(antiepidemicRecordVo, null);
        }
        return modelAndView;
    }

    /**
     * 跳转外出登记（写字楼）
     *
     * @param token    用户信息
     * @param applyId  应用ID
     * @param personId 住户ID
     * @return ReturnResult
     * @author rong yuan
     * @date 2020-2-12 2:24:31
     */
    @BaseLog(module = "e门通", classz = "社区出入信息", methods = "跳转外出登记（写字楼）", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/phone/antiepidemic/record/work/out", method = RequestMethod.GET)
    public ModelAndView recordWorkOut(String token, String applyId, String personId) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("token", token);
        modelAndView.addObject("applyId", applyId);
        modelAndView.addObject("personId", personId);

        //校验是否已经进入
        AntiepidemicRecord antiepidemicRecord = new AntiepidemicRecord();
        antiepidemicRecord.setApplyId(applyId);
        antiepidemicRecord.setPersonId(personId);
        antiepidemicRecord.setRemoveTag(DictCode.BASE_REMOVE_TAG_NORMAL);
        List<AntiepidemicRecord> antiepidemicRecordList = antiepidemicRecordService.select(antiepidemicRecord);
        if (CollectionUtils.isEmpty(antiepidemicRecordList)) {
            modelAndView.setViewName("/mobile/antiepidemic/work/antiepidemicOuted");
            return modelAndView;
        }
        antiepidemicRecordList.sort((o1, o2) -> {
            if (o1.getBeginTime().getTime() > o2.getBeginTime().getTime()) {
                return -1;
            } else if (o1.getBeginTime().getTime() < o2.getBeginTime().getTime()) {
                return 1;
            }
            return 0;
        });
        Date currentDate = new Date();
        antiepidemicRecord = antiepidemicRecordList.get(0);
        if (antiepidemicRecord.getEndTime() == null) {
            antiepidemicRecord.setEndTime(currentDate);
        }

        //检验外出时间是否符合要求
        AntiepidemicPerson antiepidemicPerson = antiepidemicPersonService.selectByPrimaryKey(personId);
        AntiepidemicBuilding antiepidemicBuilding = antiepidemicBuildingService.selectByPrimaryKey(antiepidemicPerson.getBuildingId());
        AntiepidemicCommunity antiepidemicCommunity = antiepidemicCommunityService.selectByPrimaryKey(antiepidemicBuilding.getCommunityId());
        Date offStartWork = DateUtil.parseDateTime(DateUtil.formatDate(antiepidemicRecord.getBeginTime()) + " " + antiepidemicCommunity.getOffStartWork().substring(0, 5) + ":00");
        Date offEndWork = DateUtil.parseDateTime(DateUtil.formatDate(antiepidemicRecord.getBeginTime()) + " " + antiepidemicCommunity.getOffEndWork().substring(0, 5) + ":00");
        Date nowDate = DateUtil.parseDateTime(DateUtil.formatDateTime(currentDate).substring(0, 16) + ":00");

        Date endTime = DateUtil.parseDateTime(DateUtil.formatDateTime(antiepidemicRecord.getEndTime()).substring(0, 16) + ":00");
        if (DateUtil.between(offStartWork, nowDate, DateUnit.MINUTE, false) < 0) {
            if (DateUtil.between(nowDate, endTime, DateUnit.MINUTE, false) < 0) {
                antiepidemicRecord.setEndTime(nowDate);
            }
            modelAndView.setViewName("/mobile/antiepidemic/work/antiepidemicOutAdvance");
            antiepidemicRecord.setAccessStatus(AntiepidemicConstant.ACCESS_STATUS_ADVANCE);
        } else if (DateUtil.between(offEndWork, nowDate, DateUnit.MINUTE, false) > 0) {
            if (DateUtil.between(offStartWork, endTime, DateUnit.MINUTE, false) < 0) {
                antiepidemicRecord.setEndTime(nowDate);
                modelAndView.setViewName("/mobile/antiepidemic/work/antiepidemicOutOver");
                antiepidemicRecord.setAccessStatus(AntiepidemicConstant.ACCESS_STATUS_OVERTIME);
            } else if (DateUtil.between(offEndWork, endTime, DateUnit.MINUTE, false) > 0) {
                modelAndView.setViewName("/mobile/antiepidemic/work/antiepidemicOutOver");
                antiepidemicRecord.setAccessStatus(AntiepidemicConstant.ACCESS_STATUS_OVERTIME);
            } else {
                if (DateUtil.between(antiepidemicRecord.getEndTime(), currentDate, DateUnit.SECOND, false) > MAX_SHOW_TIME) {
                    modelAndView.setViewName("/mobile/antiepidemic/work/antiepidemicOuted");
                } else {
                    modelAndView.setViewName("/mobile/antiepidemic/work/antiepidemicOutOk");
                    antiepidemicRecord.setAccessStatus(AntiepidemicConstant.ACCESS_STATUS_YES);
                }
            }
        } else {
            if (DateUtil.between(offStartWork, endTime, DateUnit.MINUTE, false) < 0) {
                antiepidemicRecord.setEndTime(nowDate);
            }
            if (DateUtil.between(antiepidemicRecord.getEndTime(), currentDate, DateUnit.SECOND, false) > MAX_SHOW_TIME) {
                modelAndView.setViewName("/mobile/antiepidemic/work/antiepidemicOuted");
            } else {
                modelAndView.setViewName("/mobile/antiepidemic/work/antiepidemicOutOk");
                antiepidemicRecord.setAccessStatus(AntiepidemicConstant.ACCESS_STATUS_YES);
            }
        }
        modelAndView.addObject("buildingInfo", antiepidemicCommunity.getBuildingName() + antiepidemicBuilding.getBuildingInfo() + antiepidemicPerson.getBuildingRoom());
        String beginTimeStr = DateUtil.formatDateTime(antiepidemicRecord.getBeginTime());
        modelAndView.addObject("beginTime", beginTimeStr.substring(0, 4) + "年" + beginTimeStr.substring(5, 7) + "月" + beginTimeStr.substring(8, 10) + "日" + beginTimeStr.substring(11, 16));
        String endTimeStr = DateUtil.formatDateTime(antiepidemicRecord.getEndTime());
        modelAndView.addObject("endTime", endTimeStr.substring(0, 4) + "年" + endTimeStr.substring(5, 7) + "月" + endTimeStr.substring(8, 10) + "日" + endTimeStr.substring(11, 16));
        modelAndView.addObject("offStartWork", antiepidemicCommunity.getOffStartWork().substring(0, 5));
        modelAndView.addObject("offEndWork", antiepidemicCommunity.getOffEndWork().substring(0, 5));
        antiepidemicRecordDao.updateByPrimaryKey(antiepidemicRecord);
        return modelAndView;
    }
}